package com.xyz.apis.modules.comm.controller;


import com.xyz.admin.common.entity.EvUserEntity;
import com.xyz.apis.repository.UserTokenRepository;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractController {

    @Autowired
    private UserTokenRepository userTokenRepository;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected EvUserEntity getUser() {
        return (EvUserEntity) SecurityUtils.getSubject().getPrincipal();
    }

    protected Long getUserId() {
        return getUser().getUserid();
    }

    protected String getUserPhone() {
        return getUser().getPhone();
    }

    protected String getUserToken() {
        return getUser().getToken();
    }

    /**
     * 获取用户信息, 用于不用shiro登陆认证的方法
     */
    protected EvUserEntity getUserByNoAuth(){
        HttpServletRequest httpRequest = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        //获取请求token
        String token = getRequestToken(httpRequest);
        if(token != null){
            return userTokenRepository.getToken(token);
        }else {
            return null;
        }
    }

    /**
     * 获取用户id, 用于不用shiro登陆认证的方法
     */
    protected Long getUserIdByNoAuth(){
        if(getUserByNoAuth() != null){
            return getUserByNoAuth().getUserid();
        }
        return null;
    }

    protected Map<String, Object> getParamsMap(Integer page){
        int pageNum = 9;
        if(page < 1){
            page = 1;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("offset",(page-1)*pageNum);
        params.put("limit", pageNum);
        return params;
    }

    /**
     * <p>
     * 从request中获取参数根据键值对形成Map. <br>
     * 注意:对于数组参数，只拷贝了第1个元素.<br>
     * 对于全空格的数据，仍然保留，在保存修改时可能需要.
     * </p>
     *
     * @param request
     * @return map
     * @author liuwei
     */
    protected static Map<String, Object> getParamValues(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        @SuppressWarnings("rawtypes")
        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String key = (String) names.nextElement();
            String value = request.getParameter(key);
            if (value != null && !"".equals(value)) {
                map.put(key, value);
            }
        }
        return map;
    }

    /**
     * <p>
     * 从request中获取参数根据键值对形成Map. <br>
     * 注意:对于数组参数，只拷贝了第1个元素.<br>
     * 对于全空格的数据，仍然保留，在保存修改时可能需要.
     * </p>
     *
     * @param request
     * @return map
     * @author liuwei
     */
    protected static Map<String, String> getParamStrValue(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        @SuppressWarnings("rawtypes")
        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String key = (String) names.nextElement();
            String value = request.getParameter(key);
            if (value != null && !"".equals(value)) {
                map.put(key, value);
            }
        }
        return map;
    }


    /**
     * 获取请求的token
     */
    private String getRequestToken(HttpServletRequest httpRequest){
        //从header中获取token
        String token = httpRequest.getHeader("token");

        //如果header中不存在token，则从参数中获取token
        if(StringUtils.isBlank(token)){
            token = httpRequest.getParameter("token");
        }

        return token;
    }

}
